//
//  LFListViewController.m
//  loanForYou
//
//  Created by 刘威 on 2017/8/31.
//  Copyright © 2017年 刘威. All rights reserved.
//

#import "LFListViewController.h"
#import "SYWebViewController.h"
#import "LFYHomeModel.h"
#import "LFYHomeViewController.h"

@interface LFListViewController ()<UITableViewDelegate,UITableViewDataSource,LFYHomeViewControllerSortCellDelegate> {
    NSInteger   _page;
    NSInteger   _size;
    LFYHomeModel *_categoryModel;
    UILabel     *_emptyLabel;
}

@property (strong, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *dataArray;

@end

@implementation LFListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"推荐列表";
    self.view.backgroundColor = [UIColor grayBackgroundColor];
    [self createNavigationBackItem];
    // Do any additional setup after loading the view.
    
    _page = -1;
    _size = 10;
    _dataArray = [NSMutableArray array];
    // Do any additional setup after loading the view.
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor grayBackgroundColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    WS(weakSelf);
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = -1;
        [weakSelf requestShow];
    }];
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestData];
    }];
    _tableView.mj_footer.automaticallyHidden = YES;
    [self.view addSubview:_tableView];
    [_tableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(self.view);
    }];
    [self requestShow];
    
    _emptyLabel = [[UILabel alloc] init];
    _emptyLabel.text = @"暂无适合的产品";
    _emptyLabel.textColor = [UIColor grayContentTitleColor];
    [self.view addSubview:_emptyLabel];
    [_emptyLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.mas_equalTo(self.view);
    }];
    _emptyLabel.hidden = YES;
}

- (void)requestShow {
    WS(weakSelf);
    [[SYHttpManager sharedHttpManager]requestGet:get_home_show_url parameters:nil success:^(id responseObject) {
        LFYShowModel *model = [[LFYShowModel alloc] initWithDictionary:(NSDictionary *)responseObject error:nil];
        if (model.data && [model.data boolValue]) {
//            _canShow = YES;
            [weakSelf requestData];
        } else {
            _emptyLabel.hidden = NO;
        }
//        [weakSelf requestData];
        
        [weakSelf.tableView.mj_header endRefreshing];
        [Tool setFileCache:HOMEBOOLSHOWKEY value:responseObject];
    } failure:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}
- (void)requestData {
    WS(weakSelf);
    _page++;
    [[SYHttpManager sharedHttpManager]requestGet:get_home_recomment_url parameters:@{@"page":@(_page),@"size":@(_size)} success:^(id responseObject) {
        [weakSelf.tableView.mj_header endRefreshing];
        LFYHomeModel *model = [[LFYHomeModel alloc] initWithDictionary:(NSDictionary *)responseObject error:nil];
        if (model && model.data.count) {
            _emptyLabel.hidden = YES;
            if (_page == 0) {
                [Tool setFileCache:HOMECATEGORYDATAKEY value:responseObject];
                [weakSelf.dataArray removeAllObjects];
            }
            [weakSelf.dataArray addObjectsFromArray:model.data];
            
            [weakSelf.tableView reloadData];
            if (model.data.count < _size) {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                [weakSelf.tableView.mj_footer setHidden:YES];
            } else {
                [weakSelf.tableView.mj_footer endRefreshing];
                [weakSelf.tableView.mj_footer setHidden:NO];
            }
        } else {
            _emptyLabel.hidden = NO;
        }
    } failure:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        [weakSelf.tableView.mj_footer setHidden:YES];
        if ([Tool getFileCache:HOMECATEGORYDATAKEY]) {
            
        } else {
            _page = -1;
            [weakSelf requestData];
        }
    }];
}

- (void)actionBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - table - dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LFYHomeViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"c"];
    if (!cell) {
        cell = [[LFYHomeViewControllerCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"c"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (_dataArray.count) {
        cell.data = _dataArray[indexPath.section];
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section) {
        LFYHomeModelItem *data = _dataArray[indexPath.section - 1];
        if (data.contentUrl.length ) { //&& _canShow
            SYWebViewController *vc = [[SYWebViewController alloc] init];
            vc.requestUrl = [NSURL URLWithString:data.contentUrl];
            [self.navigationController pushViewController:vc animated:YES];
        }
//        else if (indexPath.section < 3) {
//            LFYTestViewController *vc = [[LFYTestViewController alloc] init];
//            vc.topTitle = _testTitleArray[indexPath.section + 2];
//            vc.navTitle = _testTitleArray[indexPath.section + 2];
//            vc.content = _testValueArray[indexPath.section + 2];
//            [self.navigationController pushViewController:vc animated:YES];
//        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.section) {
//        return 130;
//    }
//    return 110;
    return 130;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    //    return 0.01f;
    
//    if (section == 1) {
//        return 35.0f;
//    }
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    if (section == 0) {
//        return 0.01f;
//    }
    return 10.0f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
